package com.xjh.mange.service.Impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjh.mange.mapper.ThingFinishTimeMapper;
import com.xjh.mange.mapper.ThingMapper;
import com.xjh.mange.model.Thing;
import com.xjh.mange.model.ThingFinishTime;
import com.xjh.mange.service.ThingFinishTimeService;
import com.xjh.mange.vo.Chart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 徐佳辉
 * @date 2020/12/20 16:45
 */
@Service
public class ThingFinishTimeServiceImpl extends ServiceImpl<ThingFinishTimeMapper, ThingFinishTime> implements ThingFinishTimeService {


    @Autowired
    private ThingFinishTimeMapper thingFinishTimeMapper;
    @Autowired
    private ThingMapper thingMapper;

    /**
     * 本周
     *
     * @param list
     * @return
     */
    @Override
    public Chart week(List<ThingFinishTime> list) {
        List<ThingFinishTime> thingFinishTimes = new ArrayList<>();
        DateTime dateTime = DateUtil.beginOfWeek(new Date());
        Date date = dateTime.toJdkDate();
        DateTime dateTime1 = DateUtil.endOfWeek(new Date());
        Date date1 = dateTime1.toJdkDate();
        // 范围里的所有id
        List<Integer> ids = new ArrayList<>();
        for (ThingFinishTime thingFinishTime : list) {
            if (thingFinishTime.getFinishTime().compareTo(date) >= 0 && thingFinishTime.getFinishTime().compareTo(date1) <= 0) {
                thingFinishTimes.add(thingFinishTime);
            }
            ids.add(thingFinishTime.getThingId());
        }
        // 去重
        ArrayList<Integer> distinct = CollUtil.distinct(ids);
        List<Integer> x = new ArrayList<>();
        for (Integer integer : distinct) {
            QueryWrapper<ThingFinishTime> wrapper = new QueryWrapper<>();
            wrapper.between("finish_time", date, date1).eq("thing_id", integer);
            Integer integer1 = thingFinishTimeMapper.selectCount(wrapper);
            x.add(integer1);
        }
        List<String> strings = thingIdToThing(distinct);
        Chart chart = new Chart();
        chart.setX(strings);
        chart.setY(x);
        return chart;
    }

    /**
     * 本月
     *
     * @param list
     * @return
     */
    @Override
    public Chart month(List<ThingFinishTime> list) {
        List<ThingFinishTime> thingFinishTimes = new ArrayList<>();
        DateTime dateTime = DateUtil.beginOfMonth(new Date());
        Date date = dateTime.toJdkDate();
        DateTime dateTime1 = DateUtil.endOfMonth(new Date());
        Date date1 = dateTime1.toJdkDate();
        // 范围里的所有id
        List<Integer> ids = new ArrayList<>();
        for (ThingFinishTime thingFinishTime : list) {
            if (thingFinishTime.getFinishTime().compareTo(date) >= 0 && thingFinishTime.getFinishTime().compareTo(date1) <= 0) {
                thingFinishTimes.add(thingFinishTime);
            }
            ids.add(thingFinishTime.getThingId());
        }
        // 去重
        ArrayList<Integer> distinct = CollUtil.distinct(ids);
        List<Integer> x = new ArrayList<>();
        for (Integer integer : distinct) {
            QueryWrapper<ThingFinishTime> wrapper = new QueryWrapper<>();
            wrapper.between("finish_time", date, date1).eq("thing_id", integer);
            Integer integer1 = thingFinishTimeMapper.selectCount(wrapper);
            x.add(integer1);
        }
        List<String> strings = thingIdToThing(distinct);
        Chart chart = new Chart();
        chart.setX(strings);
        chart.setY(x);
        return chart;
    }

    /**
     * 本年
     *
     * @param list
     * @return
     */
    @Override
    public Chart year(List<ThingFinishTime> list) {
        List<ThingFinishTime> thingFinishTimes = new ArrayList<>();
        DateTime dateTime = DateUtil.beginOfYear(new Date());
        Date date = dateTime.toJdkDate();
        DateTime dateTime1 = DateUtil.endOfYear(new Date());
        Date date1 = dateTime1.toJdkDate();
        // 范围里的所有id
        List<Integer> ids = new ArrayList<>();
        for (ThingFinishTime thingFinishTime : list) {
            if (thingFinishTime.getFinishTime().compareTo(date) >= 0 && thingFinishTime.getFinishTime().compareTo(date1) <= 0) {
                thingFinishTimes.add(thingFinishTime);
            }
            ids.add(thingFinishTime.getThingId());
        }
        // 去重
        ArrayList<Integer> distinct = CollUtil.distinct(ids);
        List<Integer> x = new ArrayList<>();
        for (Integer integer : distinct) {
            QueryWrapper<ThingFinishTime> wrapper = new QueryWrapper<>();
            wrapper.between("finish_time", date, date1).eq("thing_id", integer);
            Integer integer1 = thingFinishTimeMapper.selectCount(wrapper);
            x.add(integer1);
        }
        List<String> strings = thingIdToThing(distinct);
        Chart chart = new Chart();
        chart.setX(strings);
        chart.setY(x);
        return chart;
    }

    /**
     * 所有
     *
     * @param list
     * @return
     */
    @Override
    public Chart all(List<ThingFinishTime> list) {
        // 范围里的所有id
        List<Integer> ids = new ArrayList<>();
        for (ThingFinishTime thingFinishTime : list) {
            ids.add(thingFinishTime.getThingId());
        }
        // 去重
        ArrayList<Integer> distinct = CollUtil.distinct(ids);
        List<Integer> x = new ArrayList<>();
        for (Integer integer : distinct) {
            QueryWrapper<ThingFinishTime> wrapper = new QueryWrapper<>();
            wrapper.eq("thing_id", integer);
            Integer integer1 = thingFinishTimeMapper.selectCount(wrapper);
            x.add(integer1);
        }
        List<String> strings = thingIdToThing(distinct);
        Chart chart = new Chart();
        chart.setX(strings);
        chart.setY(x);
        return chart;
    }

    /**
     * 根据id获得事件名称
     *
     * @param list
     * @return
     */
    private List<String> thingIdToThing(List<Integer> list) {
        List<Thing> things = thingMapper.selectList(null);
        List<String> thing = new ArrayList<>();
        for (Integer o : list) {
            for (Thing thing1 : things) {
                if (thing1.getThingId().equals(o)) {
                    thing.add(thing1.getThing());
                }
            }
        }
        return thing;
    }
}
